package com.fei.yuan.myapplication;

import com.fei.yuan.myapplication.database.FormBean;
import com.fei.yuan.myapplication.database.FormDatabase;
import com.fei.yuan.myapplication.slice.TimeCardSlice;
import com.fei.yuan.myapplication.utils.ComponentProviderUtils;
import com.fei.yuan.myapplication.utils.DatabaseUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentProvider;
import ohos.bundle.AbilityInfo;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbility extends Ability {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0, "TAG---");
    private static final int INVALID_FORM_ID = -1;
    private static final int DEFAULT_DIMENSION_1X2 = 1;
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private static final int DEFAULT_DIMENSION_2X4 = 3;
    private static final int DEFAULT_DIMENSION_4X4 = 4;
    private long formId;
    private DatabaseHelper databaseHelper = new DatabaseHelper(this);
    private OrmContext ormContext;
    private ProviderFormInfo formInfo;


    @Override
    public void onStart(Intent intent) {
        HiLog.info(TAG, "onStart");
        super.onStart(intent);
        ormContext = databaseHelper.getOrmContext("FormBean", "FormDatabase.db", FormDatabase.class);
        // 开启服务
        Intent intentService = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(TimerAbility.class.getName())
                .build();
        intentService.setOperation(operation);
        startAbility(intentService);
        super.setMainRoute(TimeCardSlice.class.getName());
        

    }

    /**
     * 创建卡片时回调
     *
     * @param intent
     * @return ProviderFormInfo
     */
    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        HiLog.info(TAG, "onCreateForm");
        if (intent == null) {
            return new ProviderFormInfo();
        }
        //获取卡片id
        formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        } else {
            return new ProviderFormInfo();
        }

        //获取卡片名称
        String formName = "";
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }

        //获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }

        int layoutId = ResourceTable.Layout_form_image_with_info_time_2_2;
        if (dimension == DEFAULT_DIMENSION_2X4) {
            layoutId = ResourceTable.Layout_form_image_with_info_time_2_4;
        }else if (dimension == DEFAULT_DIMENSION_1X2){
            layoutId = ResourceTable.Layout_form_image_with_info_time_1_2;
        }

        formInfo = new ProviderFormInfo(layoutId, this);

        //存储卡片信息
        FormBean formBean = new FormBean(formId, formName, dimension);
        ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean, this);
        formInfo.mergeActions(componentProvider);

        if (ormContext == null) {
            ormContext = databaseHelper.getOrmContext("FormBean", "FormDatabase.db", FormDatabase.class);
        }
        try {
            DatabaseUtils.insertForm(formBean, ormContext);
        } catch (Exception e) {
            DatabaseUtils.deleteFormData(formBean.getFormId(), ormContext);
        }

        return formInfo;
    }

    /**
     * 更新卡片时回调
     *
     * @param formId
     */
    @Override
    protected void onUpdateForm(long formId) {
        HiLog.info(TAG, "onUpdateForm");
        super.onUpdateForm(formId);
    }

    /**
     * 请求删除卡片时回调
     *
     * @param formId
     */
    @Override
    protected void onDeleteForm(long formId) {
        HiLog.info(TAG, "onDeleteForm: formId=" + formId);
        super.onDeleteForm(formId);
        DatabaseUtils.deleteFormData(formId, ormContext);
    }

    @Override
    protected void onActive() {
        HiLog.info(TAG, "onActive");
        super.onActive();
    }

    @Override
    protected void onInactive() {
        HiLog.info(TAG, "onInactive");
        super.onInactive();
    }

    @Override
    protected void onBackground() {
        HiLog.info(TAG, "onBackground");
        super.onBackground();
    }

    @Override
    protected void onStop() {
        HiLog.info(TAG, "onStop");
        super.onStop();
    }

    @Override
    protected void onForeground(Intent intent) {
        HiLog.info(TAG, "onForeground");
        super.onForeground(intent);
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
    }
}
